<?php

/**
 * 客栈信息表模型
 */
class InnModel extends AutoMapModel {

    protected $tableName = 'hotel';
    protected $patchValidate = true;
    
    public $_map = array(
        'id'              => 'hotel0', //客栈ID
        'name'            => 'hotel1', //客栈名称
        'menuId'          => 'hotel2', //分类标志585
        'name_en'         => 'hotel3', //客栈英文名称
        'type'            => 'hotel4', //客栈类别
        'area'            => 'hotel5', //客栈位置/地理区域
        'class'           => 'hotel6', //客栈等级
        'uid'             => 'hotel7', //掌柜会员id
        'preferential'    => 'hotel8', //优惠政策
        'bookingPolicy'   => 'hotel9', //预订须知
        'gps'             => 'hotel10', //地图地址
        'introduction'    => 'hotel11', //客栈介绍
        'location'        => 'hotel12', //地理位置介绍
        'financial'       => 'hotel13', //财务信息
        'rules'           => 'hotel14', //取消及更改规则
        'pic1'            => 'hotel15', //相册图片1
        'pic2'            => 'hotel16', //相册图片2
        'pic3'            => 'hotel17', //相册图片3
        'pic4'            => 'hotel18', //相册图片4
        'pic5'            => 'hotel19', //相册图片5
        'facility'        => 'hotel20', //客栈设施
        'images'          => 'hotel21', //客栈图片列表
        'cooperation'     => 'hotel22', //合作方式
        'status'          => 'hotel23', //审核
        'addTime'         => 'hotel24', //加入时间
        'display_order'   => 'hotel25', //排序
        'hot'             => 'hotel26', //人气值
        'features'        => 'hotel27', //客栈特色
        'openTime'        => 'hotel28', //开业时间
        'address'         => 'hotel29', //客栈地址
        'roomCount'       => 'hotel30', //房间数量
      //'traffic'         => 'hotel31', //交通信息
        'beginTime'       => 'hotel32', //有效期开始
        'endTime'         => 'hotel33', //有效期结束
        'introduction_en' => 'hotel34', //英文介绍
        
        'truename'        => 'hotel35', //负责人姓名
        'mobile'          => 'hotel36', //负责人联系电话
        'email'           => 'hotel37', //负责人邮箱
        'idcard_number'   => 'hotel38', //负责人身份证号
        'idcard_pic'      => 'hotel39', //负责人身份证扫描件上传
        'orgname'         => 'hotel40', //单位名称
        'officephone'     => 'hotel41', //单位办公电话
        'biz_number'      => 'hotel42', //单位营业执照
        'biz_pic'         => 'hotel43', //单位证件扫描上传
        
        'shortintro'      => 'hotel44', //客栈简介
        'roomPrice'       => 'hotel45', //房型及价格
        'subject'         => 'hotel46', //客栈主题
        'recommend'       => 'hotel47', //推荐方式
    /**/'booktype'        => 'hotel48', //预订方式
        
        'truename1'       => 'hotel49', //联系人1称呼
        'mobile1'         => 'hotel50', //联系人1电话
        'email1'          => 'hotel51', //联系人1邮箱
        'mobile2'         => 'hotel52', //联系人2电话
        'email2'          => 'hotel53', //联系人2邮箱
        'truename2'       => 'hotel54', //联系人2称呼
        'ordermobile'     => 'hotel55', //收取订单短信手机
        'orderemail'      => 'hotel56', //收取订单邮箱
        'fax'             => 'hotel57', //客栈传真号
    );
    
    protected $_validate = array(
      //array('uid', 'require', '您尚未登录或登录过期，请重新登录后再操作！', Model::MUST_VALIDATE),

        array('name',          'require',   '客栈名称必须填写！'),
        array('area',          'require',   '客栈位置必须填写！'),
        array('type',          'require',   '客栈类别必须选择！'),
        array('class',         'require',   '客栈等级必须选择！'),
        array('preferential',  'require',   '优惠政策必须填写！'),
        array('bookingPolicy', 'require',   '预定须知必须填写！'),
        array('gps',           'require',   '地图位置必须选择！'),
        array('introduction',  'require',   '客栈介绍必须填写！'),
        array('location',      'require',   '地理位置介绍必须填写！'),
      //array('financial',     'require',   '财务信息必须填写！'),
        array('rules',         'require',   '取消及更改规则必须填写！'),
      //array('facility',      'require',   '客栈设施必须填写！'),
        array('cooperation',   'require',   '合作方式必须填写！'),
        array('features',      'require',   '客栈特色必须填写！'),
        array('openTime',      'require',   '开业时间必须填写！'),
        array('address',       'require',   '客栈地址必须填写！'),
      //array('traffic',       'require',   '交通信息必须填写！'),

        array('truename',      'require',   '掌柜姓名必须填写！'),
        array('mobile',        'number',    '掌柜手机号码未填写或填写不正确！'),
        array('email',         'email',     '掌柜邮箱未填写或填写不正确！'),
        array('idcard_number', 'number',    '掌柜身份证号码未填写或填写不正确！'),
        array('orgname',       'require',   '单位名称必须填写！'),
        array('officephone',   'require',   '办公座机必须填写！'),
        array('biz_number',    'number',    '营业执照号码未填写或填写不正确！'),
    );
    
    protected $_auto = array(
        array('addTime',  'time',  Model::MODEL_INSERT,  'function'),
        array('menuId',   '585',   Model::MODEL_INSERT,  'string'),
    );
    
    /**
     * 联合字段
     */
    public function unionField() {
        //子查询：最低价
        $roomDao = D('InnRoom');
        $min_price_query = $roomDao
            ->field('min('.$roomDao->_map['minprice'].')')
            ->where($roomDao->_map['innId'] . '=' . $this->_map['id'])
            ->buildSql();
        //子查询：点评数
        $reviewsDao = D('InnReviews');
        $reviews_count_query = $reviewsDao
            ->field('COUNT('.$reviewsDao->_map['rv_id'].')')
            ->where($reviewsDao->_map['product_id'] . '=' . $this->_map['id'])
            ->buildSql();
        //子查询：评分
        $reviews_score_query = $reviewsDao
            ->field('(sum('
                . $reviewsDao->_map['score_tese']
                . '+' . $reviewsDao->_map['score_place']
                . '+' . $reviewsDao->_map['score_interest']
                . '+' . $reviewsDao->_map['score_clean']
                . '+' . $reviewsDao->_map['score_staff']
                . '+' . $reviewsDao->_map['score_security']
                . ')*20)/(COUNT(*)*6)'
            )->where($reviewsDao->_map['product_id'] . '=' . $this->_map['id'])
            ->buildSql();
        //总查询
        return $this->field(
            $this->_map['id'] . ' as id,'
            .$this->_map['name'] . ' as name,'
            .$this->_map['shortintro'] . ' as shortintro,'
            .$this->_map['pic1'] . ' as pic1,'
            .$min_price_query . ' as min_price,'
            .$reviews_count_query . ' as reviews_count,'
            .$reviews_score_query . ' as reviews_score'
        );
    }
    /*
    public function _initialize() {
        parent::_initialize();
        
        $this->viewFields = array(
            $this->tableName => array(
                '_as'=>'inn',
                '_type'=>'LEFT',
                $this->_map['id'] => 'id',
                $this->_map['name'] => 'name',
                $this->_map['shortintro'] => 'shortintro',
                $this->_map['pic1'] => 'pic1',
            ),
            'hotelroom'=>array(
                '_as'=>'room',
                '_on'=>'room.hotelroom2=inn.hotel0',
                '_type'=>'LEFT',
                'min(room.hotelroom7)'=>'min_price'
            ),
            'pinglun'=>array(
                '_as'=>'r',
                '_on'=>'inn.hotel0=r.pinglun2',
                'COUNT(r.pinglun0)'=>'reviews_count',
                '(sum(r.pinglun9+r.pinglun10+r.pinglun11+r.pinglun12+r.pinglun13+r.pinglun14)*20)/(COUNT(r.pinglun0)*6)'=>'reviews_score'
            )
        );
    }
    */
}

?>